package com.hexanet.server.resources;

import java.io.InputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

import org.apache.commons.codec.digest.DigestUtils;

import com.hexanet.bom.Player;
import com.hexanet.bom.Role;
import com.hexanet.bom.Server;
import com.hexanet.server.dao.ServerDao;
import com.hexanet.server.reader.HexanetReader;

@Path("/servers")
public class ServerResource {
    
    ServerDao serverDao = new ServerDao();
    
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/create")
    public Response createServer(@Context SecurityContext sc, InputStream inputStream) {
	HexanetReader<Server> reader = new HexanetReader<Server>();
	Server s = reader.getDeserializedObject(inputStream);

	if (s.getCell() == null) {
	    System.out
		    .println("No cell set for server");
	    return Response.serverError().build();
	}

	
	serverDao.save(s);

	System.out.println("added server = " + s.getName());
	return Response.ok().build();
    }
    
    @POST
    @Path("/save")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response saveServer(@Context SecurityContext sc,
	    InputStream inputStream)
    {
	// has to be implemented 
	return Response.serverError().build();
    }
    
    @DELETE
    @Path("/remove/{id}")
    public Response removeServer(@Context SecurityContext sc,
	    @PathParam("id") Long id)
    {
	
	serverDao.delete(serverDao.load(id));
	
	System.out.println("after delete");
	
	return Response.ok().build();
	
    }
    
    


}
